import csv
import json

from flask import  Flask,render_template

app=Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

# 各月销售数据可视化
@app.route('/sales_month')
# TODO读取data下面的sales_month中的数据
def sales_month():
    data = []
    with open("data/sales_month.csv","r",encoding="utf8") as f:
        reader = csv.reader(f)
        next(reader)
        for row in reader:
           data.append({"month":row[0],"sales":row[1],"profit":row[2]})
    return {'data':data}

#各地区销售数据可视化
@app.route('/sales_region')
def sales_region():
    data=[]
    with open('data/sales_region.csv','r',encoding='utf8') as f:
        red=csv.reader(f)
        next(red)
        for row in red:
            data.append({'region':row[0],'sales':row[1],'profit':row[2]})
    return {'data':data}


#各产品销售数据可视化
@app.route('/sales_product')
def sales_product():
    with open('data/sales_product.json','r', encoding='utf8') as f:
        data=json.load(f)
    return {'data':data}



#各省份销售数据可视化
@app.route('/sales_province')
def sales_province():
    data=[]
    with open('data/sales_province.csv','r',encoding='utf8') as f:
        head=csv.reader(f)
        next(head)
        for row in head:
            data.append({'province':row[0],'sales':row[1],'profit':row[2]})
        return {'data':data}

#各产品经历销售数据可视化
@app.route('/sales_manager')
def sales_manager():
    data=[]
    with open('data/sales_manager.csv','r',encoding='utf8') as f:
        head=csv.reader(f)
        next(head)
        for row in head:
            data.append({'sales_manager':row[0],'sales':row[1],'profit':row[2]})
        return {'data':data}



if __name__ =='__main__':
    app.run()